unit clsTipoPedido;

interface

uses Dm, dbtables, biblio, sysutils;

type
  TTipoPedido = class(TObject)
  private
    Tipp_codigo: string;
  public
    Tipp_descricao: string;
    Codf_DentroUF: string;
    Codf_ForaUF: string;
    Hist_Codigo: string;
    Hist_CodigoF: string;
    Oper_Codigo: Integer;
    Oper_CodigoF: Integer;
    function Gravar(p_Tipp_codigo: string): Byte;
    function Recuperar(p_Tipp_codigo: string): Byte;
    function Excluir(p_Tipp_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

uses DB;

function TTipoPedido.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM TipoPedido where Tipp_codigo=:k_Tipp_codigo');
  tq.parambyname('k_Tipp_codigo').asstring := strzero(p_Tipp_codigo, 2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO TipoPedido VALUES(:k_Tipp_codigo,:k_Tipp_descricao,:k_Codf_DentroUf,:k_Codf_ForaUf,:k_Hist_Codigo,:k_Hist_CodigoF,:k_Oper_Codigo,:k_Oper_CodigoF)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE TipoPedido SET Tipp_descricao=:k_Tipp_descricao, CODF_DentroUF =:k_Codf_DentroUf,' +
      'CODF_ForaUF =:k_Codf_ForaUf, Hist_Codigo =:k_Hist_Codigo, Hist_CodigoF = :k_Hist_CodigoF, ' +
      'Oper_Codigo =:k_Oper_Codigo, Oper_CodigoF =:k_Oper_CodigoF ' +
      'WHERE Tipp_codigo=:k_Tipp_codigo');
  end;
  tq.parambyname('k_Tipp_codigo').asstring := strzero(p_Tipp_codigo, 2);
  tq.parambyname('k_Tipp_descricao').asstring := Tipp_descricao;
  tq.ParamByName('k_Codf_DentroUf').AsString := Codf_DentroUF;
  tq.ParamByName('K_Codf_ForaUf').AsString := Codf_ForaUF;
  tq.ParamByName('k_Hist_Codigo').AsString := Hist_Codigo;
  tq.ParamByName('k_Hist_CodigoF').AsString := Hist_CodigoF;
  tq.ParamByName('k_Oper_Codigo').AsInteger := Oper_Codigo;
  tq.ParamByName('k_Oper_CodigoF').AsInteger := Oper_CodigoF;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TTipoPedido.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM TipoPedido where Tipp_codigo=:k_Tipp_codigo');
  tq.Params[0].asstring := strzero(p_Tipp_codigo, 2);
  tq.open;
  Tipp_codigo := p_Tipp_codigo;
  if tq.eof then begin
    Tipp_descricao := '';
    Codf_DentroUF := '';
    Codf_ForaUF := '';
    Hist_Codigo := '';
    Hist_CodigoF := '';
    Oper_Codigo := -1;
    Oper_CodigoF := -1;
    Result := 0;
  end else begin
    Tipp_descricao := tq.fieldbyname('Tipp_descricao').asstring;
    Codf_DentroUF := tq.fieldbyname('Codf_DentroUf').asstring;
    Codf_ForaUF := tq.fieldbyname('codf_ForaUf').asstring;
    Hist_Codigo := tq.FieldByName('Hist_Codigo').AsString;
    Hist_CodigoF := tq.FieldByName('Hist_CodigoF').AsString;
    Oper_Codigo := StrToIntDef(tq.FieldByName('Oper_Codigo').AsString, -1);
    Oper_CodigoF := StrToIntDef(tq.FieldByName('Oper_CodigoF').AsString, -1);
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TTipoPedido.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM TipoPedido WHERE Tipp_codigo=:k_Tipp_codigo');
  tq.parambyname('k_Tipp_codigo').asstring := strzero(p_Tipp_codigo, 2);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TTipoPedido.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from TipoPedido where Tipp_descricao>=:k_Tipp_descricao order by Tipp_descricao');
  p_q.parambyname('k_Tipp_descricao').asstring := p_nome;
end;

end.

